package com.mc.moring.BafBase

import android.app.Activity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.mc.moring.R
import com.nadia.totoro.base.IBaseFragment
import com.nadia.totoro.view.IView
//import kotlinx.android.synthetic.main.actionbar_custom_top.topView
import org.greenrobot.eventbus.EventBus

/**
 *
 * author: Created by nao on 2018-09-12
 * version: 1.0.0
 */
abstract class BAFBaseFragment<A : IBaseActivity<A>, F : Fragment> : IBaseFragment<A, F>(), IView {

    lateinit var spotsDialog: SpotsDialog
    lateinit var mApplication: MyApplication

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mApplication = act.application as MyApplication
    }

    /**
     * 	动态设置状态栏高度
     */
//    fun initTopView() {
//        if (topView != null) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//                topView.visibility = View.VISIBLE
//                //获取当前控件的布局对象
//                val params = topView.layoutParams as LinearLayout.LayoutParams
//                //设置当前控件布局的高度
//                params.height = StatusBarUtils.getStatusBarHeight(act)
//                //将设置好的布局参数应用到控件中
//                topView.layoutParams = params
//            } else {
//                topView.visibility = View.GONE
//            }
//        }
//    }

    override fun showLoading(msg: String?) {
        spotsDialog = SpotsDialog(activity, if (msg.isNullOrEmpty()) getString(R.string.please_wait) else msg)
        spotsDialog.setCancelable(true)
        spotsDialog.setCanceledOnTouchOutside(true)
        spotsDialog.show()
    }

    override fun hideLoading() = spotsDialog.dismiss()

    override fun toastShowShort(rId: Int) = act.toastShow(rId)

    override fun toastShowShort(msg: String) = act.toastShow(msg)

    override fun showError(imageId: Int, text: String, status: Int) {}

    override fun showEmpty() {}

    override fun getContextView(): Activity = requireActivity()

    /**
     * 事件
     */
    fun registerEventBus(subscriber: Any) {
        if (!EventBus.getDefault().isRegistered(subscriber)) EventBus.getDefault().register(subscriber)
    }

    fun unregisterEventBus(subscriber: Any) {
        if (EventBus.getDefault().isRegistered(subscriber)) EventBus.getDefault().unregister(subscriber)
    }

    /**
     * 用户相关
     */
    fun userId(): String = mApplication.userId
}
